package routines;

import java.util.List;

import org.hibernate.Session;

public class DAOLegalEntity extends DAO {

	public static LegalEntity getByInnKpp(String inn, String kpp)
			throws Exception {
		CheckSession();
		if (inn == null)
			throw new Exception("inn can not be null!");

		List<LegalEntity> le = (List<LegalEntity>) session.getNamedQuery("LegalEntity.getByInnKpp")
				//.createQuery("from LegalEntity where inn = :inn and kpp= :kpp and ogrn is null")
				.setString("inn", inn.trim())
				.setString("kpp", (kpp == null ? "-" : kpp).trim()).list();
//		if (le == null) System.err.println("not found " + inn + "/" + kpp);
		return (le == null || le.size() == 0)?null:le.get(0);
	}

	public static LegalEntity getByOgrn(String ogrn) throws Exception {
		CheckSession();
		if (ogrn == null)
			throw new Exception("ogrn can not be null!");
		LegalEntity le = (LegalEntity) session.getNamedQuery("LegalEntity.getByOgrn")
				//.createQuery("from LegalEntity where ogrn = :ogrn")
				.setString("ogrn", ogrn).uniqueResult();
		return le;
	}

	public static LegalEntity getByInnKppOgrn(String inn, String kpp,
			String ogrn) throws Exception {
		CheckSession();

		if (ogrn == null || inn == null || kpp == null)
			throw new Exception(new StringBuilder()
					.append("Input parametrs can not be null!").append("inn=")
					.append(inn).append(", kpp=").append(kpp).append(", ogrn=")
					.append(ogrn).toString());
		LegalEntity le = (LegalEntity) session.getNamedQuery("LegalEntity.getByInnKppOgrn")
				//.createQuery("from LegalEntity where ogrn = :ogrn and inn= :inn and kpp = :kpp")
				.setString("ogrn", ogrn).setString("inn", inn)
				.setString("kpp", kpp).uniqueResult();
		return le;
	}

}
